/******************************************************************************************************************
* File:SourceFilter.java
* Course: 17655
* Project: Assignment 1
* Copyright: Copyright (c) 2003 Carnegie Mellon University
* Versions:
*	1.0 November 2008 - Sample Pipe and Filter code (ajl).
*
* Description:
*
* This class serves as a source filter. This  filter is 
* a source filter that reads some input from the FlightData.dat file and writes the bytes up stream.
******************************************************************************************************************/

import java.io.*;

public class SourceFilter extends FilterFramework2
{
	// Internal Storage for filename
	private String fileName;

	// Constructor
	// Takes filename of the input file
	public SourceFilter(String argument)
	{
		super(0,1);//zero inputs, one output
		fileName = argument;
	}
	
	// Run Method
	public void run()
    {
		DataInputStream in = null;			// File stream reference.
		byte databyte = 0;					// The byte of data read from the file
		
		System.out.println("Frame length is " + Measurement.framelength);

		try
		{
			in = new DataInputStream(new FileInputStream(fileName));
			System.out.println("\n" + this.getName() + "::Source reading file..." );

			// read bytes and write to output until the file is finished
			while(true)
			{
				databyte = in.readByte();
				WriteFilterOutputPort(databyte, 0);
			} // while

		} //try
		
		// End of file
		catch ( EOFException eoferr )
		{
			System.out.println("\n" + this.getName() + "::End of file reached..." );
			try
			{
				in.close();
				System.out.println( "\n" + this.getName() + "::Read file complete" );
			}
			//Problem closing file
			catch (Exception closeerr)
			{
				System.out.println("\n" + this.getName() + "::Problem closing input data file::" + closeerr);

			} // catch

		} // catch
		// Problem opening a file
		catch ( IOException iox )
		{
			System.out.println("\n" + this.getName() + "::Problem reading input data file::" + iox );

		} // catch
   } // run
} // SourceFilter